using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace xna_game_engine.source
{
    public class InputSystem : Microsoft.Xna.Framework.GameComponent
    {
        // Members
        private KeyboardState kbState, prevKbState;
        private MouseState mouseState, prevMouseState;
        private GamePadState gamePad1State, prevGamePad1State;

        public InputSystem(Game game) : base(game)
        {
            // TODO: Construct any child components here
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code herekbState = new KeyboardState();
            prevKbState = new KeyboardState();
            mouseState = new MouseState();
            prevMouseState = new MouseState();
            gamePad1State = new GamePadState();
            prevGamePad1State = new GamePadState();

            //  TODO: Add support for more players
            

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            prevKbState = kbState;
            prevMouseState = mouseState;
            prevGamePad1State = gamePad1State;
            base.Update(gameTime);
        }
        /// <summary>
        /// Be sure to call this at the beginning of your update loop as
        /// it grabs the current state of the keyboard/mouse, and match it 
        /// with a call to Update() at the end of the of your Update()
        /// </summary>
        public void PollInputDevices()
        {
            kbState = Keyboard.GetState();
            mouseState = Mouse.GetState();
            gamePad1State = GamePad.GetState(PlayerIndex.One);
        }

        public bool CheckKeyPress(Keys key)
        {
            return (kbState.IsKeyDown(key) && prevKbState.IsKeyUp(key));
        }

        public bool CheckKeyPressContinuous(Keys key)
        {
            return (kbState.IsKeyDown(key));
        }

        public bool CheckButtonPress(Buttons button)
        {
            return (gamePad1State.IsButtonDown(button) && prevGamePad1State.IsButtonUp(button));
        }

        public bool CheckButtonPressContinuous(Buttons button)
        {
            return (gamePad1State.IsButtonDown(button));
        }

        public bool CheckLeftMouseButton()
        {
            return (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton == ButtonState.Released);
        }

        #region Properties
        public MouseState MouseInfo
        {
            get { return mouseState; }
        }
        #endregion
    }
}